/**
 * multer配置文件
 * 配置图片上传目录和处理文件名
 */
// import { MulterOptions } from '@nestjs/platform-express';
// import multer from 'multer';
// import * as path from 'path';
import { MulterModuleOptions } from '@nestjs/platform-express';
import * as fs from 'fs';
import { diskStorage } from 'multer'
import { extname, basename, join } from 'path'

export const storageConfig: MulterModuleOptions = {
    storage: diskStorage({
        destination: (req, file, cb) => {
            // 配置上传目录
            const dir = join(__dirname, '../uploads')
            console.log('经过multer配置文件--目录', dir)

            if (!fs.existsSync(dir)) {
                fs.mkdirSync(dir)
            }
            cb(null, dir)
        },
        filename: (req, file, cb) => {
            console.log('经过multer配置文件', file)
            // index.html -> .html 就是拿到文件后缀名
            const ext = extname(file.originalname);
            // path.basename('/foo/bar/baz/asdf/quux.html');
            // Returns: 'quux.html'
            const name = basename(file.originalname, ext);
            const filename = `${name}_${Date.now()}${ext}`;
            return cb(null, filename)
        }

    })
}